<?php
namespace Mobile\Controller;
use Think\Log;

class InsuranceController extends MobileController {
    public function index(){
        echo 'you are error!!';
    }
    
    /**
    * 获取保险费率参数,供脚本调用
    * 
    * @param mixed $company 保险公司ID，暂时未使用
    */
    public function getParams($company=''){
        //载入系统参数
        $config =   api('Config/lists');
        C($config);
        
        //第三者责任险
        $dsz = array();
        $fl_dsz = C('FL_DSZZRX');
        foreach($fl_dsz as $fl){
            $dsz[] = explode('|',$fl);
        }

        //车辆损失险
        $clss = array();
        $fl_clss = C('FL_CLSSX');
        foreach($fl_clss as $fl){
            $item = array();
            $flArr = explode('||',$fl);
            foreach($flArr as $fl1){
                $item[] = explode('|',$fl1);
            }
            $clss[] = $item;
        }
        
        //车上人员责任险
        $csry = array();
        $fl_csry = C('FL_CSRYZRX');
        foreach($fl_csry as $fl){
            $csry[] = explode('|',$fl);
        }

        //盗抢险
        $dqx = array();
        $fl_dqx = C('FL_DQX');
        foreach($fl_dqx as $fl){
            $dqx[] = explode('|',$fl);
        }
        
        //玻璃单独破碎险
        $bl = array();
        $fl_bl = C('FL_BLDDPSX');
        foreach($fl_bl as $fl){
            $bl[] = explode('|',$fl);
        }       
        
        //车身划痕险
        $cshh = array();
        $fl_hh = C('FL_CSHHSSX');
        foreach($fl_hh as $fl){
            $item = array();
            $flArr = explode('||',$fl);
            foreach($flArr as $fl1){
                $item[] = explode('|',$fl1);
            }
            $cshh[] = $item;
        }
        
        //自燃险
        $zrx = C('FL_ZRSSX');
        
        //涉水发动机损坏保险
        $ssx = C('FL_SSX');

        //不计免赔特约条款
        $bjmp = C('FL_BJMP');

        //交强险
        $jqx = array();
        $fl_jqx = C('FL_JQX');
        foreach($fl_jqx as $fl){
            $jqx[] = explode('|',$fl);
        }
        
        //车船税
        $ccs = C('FL_CCS');
        
        //优惠系数
        $yh = C('FL_YHFL');

        $ret = array(
            'dsz' => $dsz,
            'clss' => $clss,
            'csry' => $csry,
            'dqx' => $dqx,
            'bl' => $bl,
            'cshh' => $cshh,
            'zrx' => $zrx,
            'ssx' => $ssx,
            'bjmp' => $bjmp,
            'jqx' => $jqx,
            'ccs' => $ccs,
            'yh' => $yh,
        );
        $this->succ($ret);
    }
    
    /**
    * 获取区域列表
    * 
    */
    public function area(){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }
        $model = M('ProvinceArea');
        $where['status'] = 1;
        $res = $model->where($where)->order('car_prefix')->select();
        $this->succ($res);
    }
    
    /**
    * 车辆车型查询
    * 
    */
    public function getVehicleInfo(){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }
        $params = I('post.');
        $cicService = D('Cic','Service');
        $ret = $cicService->getVehicleInfo($params);
        Log::record(print_r($ret,true),Log::DEBUG);
        if($ret){
            $this->succ($ret);
        }else{
            $this->err($cicService->error);
        }
    }
    
    /**
    * 获取车险承保方案信息
    * 
    */
    public function getScheme(){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }
        $params = I('post.');        
        $cicService = D('Cic','Service');
        $ret = $cicService->getScheme($params);
        Log::record(print_r($ret,true),Log::DEBUG);
        if($ret){
            $this->succ($ret);
        }else{
            $this->err($cicService->error);
        }
    }

    /**
    * 计算保费
    *     
    */
    public function calcFee(){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }
        $params = I('post.');
        $cicService = D('Cic','Service');
        $ret = $cicService->calcFee($params);
        Log::record(print_r($ret,true),Log::DEBUG);
        if($ret){
            $this->succ($ret);
        }else{
            $this->err($cicService->error);
        }
    }
    
    /**
    * 信息校验
    * 
    */
    public function verifyInfo(){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }
        $params = I('post.');
        $cicService = D('Cic','Service');
        $ret = $cicService->verifyInfo($params);
        Log::record(print_r($ret,true),Log::DEBUG);
        if($ret){
            $this->succ($ret);
        }else{
            $this->err($cicService->error);
        }
    }
    
    /**
    * 投保确认
    * 
    */
    public function confirm(){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }
        $params = I('post.');
        $cicService = D('Cic','Service');
        $ret = $cicService->confirm($params);
        Log::record(print_r($ret,true),Log::DEBUG);
        if($ret){
            $this->succ($ret);
        }else{
            $this->err($cicService->error);
        }
    }
    
    /**
    * 申请购买
    * 
    */
    public function buy(){
        if(empty($this->uid)){
            $this->err('用户未登录！');
        }
        $params = I('post.');
        $cicService = D('Cic','Service');
        $ret = $cicService->buy($params);
        Log::record(print_r($ret,true),Log::DEBUG);
        if($ret){
            //TODO:转移保存到数据库记录中的操作到公共模块
            $modelCic = M('CicOrder');
            unset($where);
            $where['c_order_no'] = I('c_order_no');
            $resCic = $modelCic->where($where)->find();
            if($resCic){
                $product_id = 1; //目前仅有一种
                $modelProduct = M('Product');
                $resProduct = $modelProduct->where("product_id=$product_id")->find();
                if($resProduct){//记录订单，获取日收益和日冻结收益金额
                    $tableName = $resProduct['order_table'];
                    $day_amt_fun = $resProduct['day_amt_fun'];
                    $day_frozen_amt_fun = $resProduct['day_frozen_amt_fun'];
                    $primary_key = $resProduct['primary_key'];
                    $targetId = $resCic['corder_id'];
                    
                    $modelTable = M($tableName);
                    $resAmt = $modelTable->query("select ($day_amt_fun) as day_amt,($day_frozen_amt_fun) as day_frozen_amt from __TABLE__ where $primary_key=$targetId");
                    if(!empty($resAmt)){
                        $day_amt = $resAmt[0]['day_amt'];
                        $day_frozen_amt = $resAmt[0]['day_frozen_amt'];
                    }else{
                        $day_amt = 0;
                        $day_frozen_amt = 0;
                    }
                    
                    $modelOrder = M('Order');
                    unset($data);
                    $data['order_no'] = $resCic['c_order_no'];
                    $data['uid'] = $this->uid;
                    $data['product_id'] = $product_id;
                    $data['posttime'] = date('Y-m-d H:i:s');
                    $data['car_no'] = $resCic['c_lcn_no'];//记录车牌号码
                    $data['targetid'] = $targetId;
                    $data['sendtype'] = $sendtype;
                    $data['start_date'] = $resCic['t_insrnc_bgn_tm'];
                    $data['end_date'] = $resCic['t_insrnc_end_tm'];
                    $data['day_amt'] = $day_amt;
                    $data['day_frozen_amt'] = $day_frozen_amt;
                    $data['n_real_amt'] = $resCic['n_real_amt'];
                    $data['n_traff_real_prm'] = $resCic['n_traff_real_prm'];
                    $data['n_tax_real_prm'] = $resCic['n_tax_real_prm'];
                    $modelOrder->add($data);
                    
                    //自动增加车辆信息
                    $modelCar = M('UserCar');
                    unset($where);
                    $where['uid'] = $this->uid;
                    $where['c_lcn_no'] = $resCic['c_lcn_no'];
                    $resCar = $modelCar->where($where)->find();
                    unset($data);
                    $data['uid'] = $this->uid;
                    $data['c_vehicle_brand'] = $resCic['c_vehicle_brand'];
                    $data['c_brnd_cde'] = $resCic['c_brnd_cde'];
                    $data['c_vehicle_name'] = $resCic['c_vehicle_name'];
                    $data['c_fee_atr'] = $resCic['c_fee_atr'];
                    $data['c_lcn_no'] = $resCic['c_lcn_no'];
                    $data['c_vin_cde'] = $resCic['c_vin_cde'];
                    $data['c_vhl_frm'] = $resCic['c_vhl_frm'];
                    $data['c_eng_no'] = $resCic['c_eng_no'];
                    $data['c_fst_reg_dte'] = $resCic['c_fst_reg_dte'];
                    $data['n_limit_load_person'] = $resCic['n_limit_load_person'];
                    $data['c_whole_weight'] = $resCic['c_whole_weight'];
                    $data['c_ext_msr'] = $resCic['c_ext_msr'];
                    $data['c_power_type'] = $resCic['c_power_type'];
                    $data['n_vehicle_price'] = $resCic['n_vehicle_price'];
                    $data['c_import_flag'] = $resCic['c_import_flag'];
                    if($resCar){
                        $modelCar->where("car_id={$resCar['car_id']}")->save($data);
                    }else{
                        $modelCar->add($data);
                    }
                }
            }   
            $this->success($ret,C('CIC_WAP_PAY_URL').I('post.c_order_no'),true);
        }else{
            $this->err($cicService->error);
        }
    }
    
}